package com.st.mall.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.st.mall.product.model.CateModel;
import com.st.mall.product.model.ProductModel;
import com.st.mall.product.service.ICateService;
import com.st.mall.product.service.IProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private IProductService service;
	@Autowired
	private ICateService cateService;
	
	@RequestMapping("/insert")
	@ResponseBody
	public String insert(ProductModel productModel) {
		System.out.println(productModel.toString());
		String result = service.insert(productModel);
		return result;
	}
	
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(ProductModel productModel) {
		System.out.println(productModel.toString());
		List<ProductModel> list = service.selecctModel(productModel);
		int result = service.selectCount(productModel);
		Map<String,Object> map = new HashMap<>();
		map.put("data", list);
		map.put("count", result);
		return new JSONObject(map).toString();
	}
	
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String get(String code) {
		ProductModel result = service.selectId(code);
		return new JSONObject(result).toString();
	}
	
	@RequestMapping(value = "/getOne",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getOne(String name) {
		ProductModel result = service.selectName(name);
		if(result==null)
			return "0";
		return new JSONObject(result).toString();
	}
	
	@RequestMapping(value = "/update",produces = "application/json;charset=utf-8")
	@ResponseBody
	public int update(ProductModel productModel) {
		int result = service.update(productModel);
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(String code) {
		int result = service.delete(code);
		return result;
	}
	
	@RequestMapping("/updateLevel")
	@ResponseBody
	public int updateLev(ProductModel productModel) {
		int result = service.updateLev(productModel);
		return result;
	}
	
	@RequestMapping(value = "/selectAll",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectAll(String code) {
		List<ProductModel> list = new ArrayList<>();
		int result = 0;
		List<CateModel> li = cateService.selectAll(code);
		for(CateModel model:li) {
			ProductModel productModel = new ProductModel();
			productModel.setParentCode(model.getCode());
			list.addAll(service.selecctModel(productModel));
			result += service.selectCount(productModel);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("data", list);
		map.put("count", result);
		return new JSONObject(map).toString();
	}
}
